import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { UsersService } from 'src/modules/users/users.service'
import { JwtService } from '@nestjs/jwt';
import { User } from 'src/interface/userInterface';
@Injectable()
export class AuthService {
    constructor(
        // 注入一个静态 只读 的用户类
        private readonly usersService: UsersService,
        private readonly jwtService: JwtService
    ) { }

    /**
     * @jwt认证登录
     * @param user
     */
    async login(createUserDto: User) {
        const user = await this.usersService.userLogin(createUserDto)
        if (user.status == 200) {
            const payload = { username: createUserDto.username, password: createUserDto.password }
            const res = {
                status: user.status,
                message: user.message,
                data: this.jwtService.sign(payload)
            }
            throw new HttpException(res, HttpStatus.OK);
        } else {
            const res = {
                status: user.status,
                message: user.message,
                data: [],
            }
            throw new HttpException(res, HttpStatus.OK);
        }
    }
    
    /**
     * @token认证
     * @param user
     */
    async verifyToken(accessToken:string) {
        
    }
}
